﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace WDTAsg2.DbBizObject
{
    public class ProductInfo
    {

        /// <summary>
        /// Get all products belonging in a specific categoru
        /// </summary>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public static SqlDataReader GetProductsByCategory(int categoryId)
        {
            string conString = ConfigurationManager.ConnectionStrings["HardwaresRUsCS"].ConnectionString;

            // Stored procedure
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand("getProductsByCategory", con);
            cmd.CommandType = CommandType.StoredProcedure;

            // Param
            cmd.Parameters.AddWithValue("@CatId", categoryId);

            con.Open();
            SqlDataReader dtr = cmd.ExecuteReader();

            //con.Close();

            return dtr;
        }

        /// <summary>
        /// Get products by their ID
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public static SqlDataReader GetProductById(int productId)
        {
            string conString = ConfigurationManager.ConnectionStrings["HardwaresRUsCS"].ConnectionString;

            //Stored proc
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand("getProductById", con);
            cmd.CommandType = CommandType.StoredProcedure;
            // Param
            cmd.Parameters.AddWithValue("@ProdId", productId);

            con.Open();
            SqlDataReader dtr = cmd.ExecuteReader();

            //con.Close();

            return dtr;
        }


        /// <summary>
        /// Get all products
        /// </summary>
        /// <returns></returns>
        public static SqlDataReader GetAllProducts()
        {
            string conString = ConfigurationManager.ConnectionStrings["HardwaresRUsCS"].ConnectionString;
            // Stored proc
            SqlConnection con = new SqlConnection(conString);
            SqlCommand cmd = new SqlCommand("getAllProducts", con);
            cmd.CommandType = CommandType.StoredProcedure;

            con.Open();
            SqlDataReader dtr = cmd.ExecuteReader();

            //con.Close();

            return dtr;
        }

    }
}